package com.jnevision.banner.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crtland.landoss.LandOss;
import com.crtland.landoss.Oss;
import com.crtland.landoss.OssInfo;
import com.crtland.landoss.OssType;
import com.crtland.landoss.PathEnum;
import com.crtland.landoss.Result;
import com.jnevision.banner.model.Banner;
import com.jnevision.banner.service.BannerServiceImpl;
import com.jnevision.dept.model.Dept;
import com.jnevision.dept.service.DeptServiceImpl;
import com.jnevision.util.Base64Util;

/**
 * <p>
 * banner图 前端控制器
 * </p>
 *
 * @author gavin
 * @since 2019-07-14
 */

@RestController
@RequestMapping("/banners")
public class BannerController extends ApiController {
    @Autowired
    private BannerServiceImpl bannerServiceImpl;
    @Autowired
    private DeptServiceImpl deptServiceImpl;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private OssInfo ossInfo;

    @PostMapping
    public R<Boolean> add(@RequestBody Banner banner) {
        banner.setValid(true);
        LandOss landOss = new LandOss(redisTemplate);
        Oss oss = landOss.getLandOss(OssType.ALI, ossInfo);

        MultipartFile file = Base64Util.base64ToMultipart(banner.getUrl());

        try {
            Result result = oss.upload(PathEnum.MATERIAL, file);
            if (result.getStatus()) {
                banner.setUrl(result.getFileUrl());
            } else {
                return this.failed("banner图上传失败，请重试");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return this.success(this.bannerServiceImpl.save(banner));
    }

    @PutMapping
    public R<Boolean> update(@RequestBody Banner banner) {
        LandOss landOss = new LandOss(redisTemplate);
        Oss oss = landOss.getLandOss(OssType.ALI, ossInfo);

        MultipartFile file = Base64Util.base64ToMultipart(banner.getUrl());

        try {
            Result result = oss.upload(PathEnum.MATERIAL, file);
            if (result.getStatus()) {
                banner.setUrl(result.getFileUrl());
            } else {
                return this.failed("banner图上传失败，请重试");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return this.success(this.bannerServiceImpl.updateById(banner));
    }

    @GetMapping
    public R<Banner> get(int id) {
        return this.success(this.bannerServiceImpl.getById(id));
    }

    @DeleteMapping
    public R<Boolean> delete(int id) {
        return this.success(this.bannerServiceImpl.removeById(id));
    }

    /**
     * 根据部门获取所需banner图
     * 
     * @param deptIds
     * @return
     */
    @GetMapping("/dept")
    public R<List<Banner>> getByDept(int deptId) {
        return this.success(this.bannerServiceImpl.list(new QueryWrapper<Banner>().eq("dept_id", deptId)));
    }

    /**
     * 根据当前用户获取所需banner图
     * 
     * @param token
     * @return
     */
    @GetMapping("/user")
    public R<List<Banner>> getByDept(@RequestHeader("token") String token) {
        Dept dept = this.deptServiceImpl.getProject(token);

        if (dept == null) {
            return this.success(this.bannerServiceImpl.list(new QueryWrapper<Banner>().eq("dept_id", -1))); // 返回默认的banner图，需要提前设置
        }

        return this.success(this.bannerServiceImpl.list(new QueryWrapper<Banner>().eq("dept_id", dept.getId())));
    }

    /**
     * 获取所有的banner图
     * 
     * @param page
     * @param banner
     * @return
     */
    @GetMapping("/page")
    public R<IPage<Banner>> page(Page<Banner> page, Banner banner) {
        return this.success(this.bannerServiceImpl.page(page, new QueryWrapper<Banner>(banner)));
    }
}
